﻿// Matt's TinyMVC Copyright (c) 2009 Matt J. Crouch

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MattsTinyMVCTest
{
    [TestClass]
    public class TinyMVCControllerTests
    {
        [TestMethod]
        public void ControllerTest()
        {
            TestController testController = new TestController();
            testController.Description = "Test";

            for (int i = 0; i < 1000; i++)
            {
                TestModel testModel = new TestModel()
                {
                    Property1 = i,
                    Property2 = "Test Model #" + i.ToString(),
                    Property3 = DateTime.Now,
                    Property4 = true,
                    Property5 = 3.14M
                };

                testController.AddNewModel(testModel);

                for (int j = 0; j < 10; j++)
                    testModel.AddNewModel();
            }
            bool isModelAdditionsCountCorrectBeforeFullRefresh = testController.ModelAdditions.Count() == 11000;
            bool isModelRemovalsZeroBeforeFullRefresh = testController.ModelRemovals.Count() == 0;
            bool isModelLocatorCountZeroBeforeFullRefresh = testController.ModelLocator.Count() == 0;

            testController.RefreshModelLocator(true);

            bool isModelLocatorCountCorrectAfterFullRefresh = testController.ModelLocator.Count() == 11001;
            bool didAllModelCommandsExecute = true;
            foreach (var model in testController.ModelLocator)
            {
                testController.DispatchCommand("TestCommand", model.Key);

                TestModel tm = model.Value as TestModel;
                if (tm != null && tm.Property1 <= 0)
                {
                    didAllModelCommandsExecute = false;
                    break;
                }

                TestController tc = model.Value as TestController;
                if (tc != null && string.IsNullOrEmpty(tc.Description))
                {
                    didAllModelCommandsExecute = false;
                    break;
                }
            }

            foreach (var model in testController.ModelLocator)
            {
                TestModel tm = model.Value as TestModel;
                if (tm != null)
                    tm.Remove();
            }

            testController.RefreshModelLocator(false);

            bool isRemovalSuccessful = testController.ModelLocator.Count() == 1;

            Assert.IsTrue(isModelAdditionsCountCorrectBeforeFullRefresh &&
                            isModelLocatorCountCorrectAfterFullRefresh &&
                            isModelRemovalsZeroBeforeFullRefresh &&
                            isModelLocatorCountCorrectAfterFullRefresh && 
                            isRemovalSuccessful &&
                            didAllModelCommandsExecute);
        }
    }
}
